<?php
namespace app\mobile\controller;

use app\model\Record; 
use app\model\Fee; 
use app\model\Comm; 
use app\model\Theme; 
use app\model\Worker; 
use app\model\Order; 

class Yaoyiyao extends Base
{
    public function Yaoyiyao()
    {
global $_G, $_P;
$system    = $_G['system'];
$fans = $this->checkuser();
$op = !empty($_P['op']) ? $_P['op'] : 'display';
$themeid = intval($_P['themeid']);
$theme = Theme::getThemeById($themeid);
$config = Theme::getConfig($themeid);
$fuid = $_G['member']['uid'];
$result = array();
$GLat= $_P['GLat'];
$GLng= $_P['GLng'];

//新版概率随机
if ($config['comm711']*100>0){
$randomfee = floatval(mt_rand($config['comm711']*100,$config['comm712']*100)/100);
}else{
$first = intval($config['comm43']);
$second= $first + intval($config['comm43_2']);
$gailv = mt_rand(0,99);
if ($first==0 || $first==100 || $gailv < $first ) {
    $randomfee = floatval(mt_rand($config['comm41']*100,$config['comm42']*100)/100);
}elseif ($gailv < $second && $config['comm42_2']!=0 ) {
    $randomfee = floatval(mt_rand($config['comm41_2']*100,$config['comm42_2']*100)/100);
}elseif ($config['comm42_3']!=0 ) {
    $randomfee = floatval(mt_rand($config['comm41_3']*100,$config['comm42_3']*100)/100);
}else{
    $randomfee = floatval(mt_rand($config['comm41']*100,$config['comm42']*100)/100);
}
}
$ClientIp = Comm::getClientIp();
$data = array(
	'uniacid'    => $_G['mpid'],
	'themeid'    => $themeid,
	'type'       => 7,
	'uid'     => $fuid,
	'openid'     => $_G['openid'],
	'addtime'    => TIMESTAMP,
	'fee'        => $randomfee,
	'ip'    => $ClientIp,
	'status'     =>0,
);

$workerid = intval($_P['wid']);
$theyworker = Worker::getThemeWorkerByUid($workerid,$themeid,1);
if (empty($theyworker)) {
	$workerid=0;
}elseif($theyworker['comm4status']>1){
	$workerid=0;
}elseif ($config['comm4typeyg']>0){
	$liulan = 0;
	$chengjiao = 0;
	switch ($config['comm4typeyg']) {
		case '1':
			$liulan = 30;
			break;
		case '2':
			$liulan = 40;
			break;
		case '3':
			$liulan = 50;
			break;
		case '4':
			$liulan = 60;
			break;
		case '5':
			$liulan = 70;
			break;
		case '6':
			$chengjiao = 5;
			break;
		case '7':
			$chengjiao = 10;
			break;
		case '8':
			$chengjiao = 15;
			break;
		case '9':
			$chengjiao = 20;
			break;
		case '10':
			$chengjiao = 25;
			break;
		case '11':
			$chengjiao = 30;
			break;
		case '12':
			$chengjiao = 40;
			break;
		case '13':
			$chengjiao = 50;
			break;
	}
	if ($theyworker['forward']>500){
		if ($liulan>0) {
			if (($theyworker['forward']/$theyworker['scansum']*100) > $liulan){
				db_query("UPDATE ".tablename(t_worker)." SET comm4status = '2' WHERE uid ='{$workerid}'" );
				$workerid = 0;
			}

		}elseif ($chengjiao>0) {
			$cjtotal = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . "
					WHERE status > 0 and workerid = '{$workerid}' and themeid = '{$themeid}' " );
			if ($theyworker['forward'] / $cjtotal > $chengjiao){
				db_query("UPDATE ".tablename(t_worker)." SET comm4status = '2' WHERE uid ='{$workerid}'" );
				$workerid = 0;
			}
		}
	}

}
if (empty($workerid)) {
	$data['msg']='没有员工信息，不可领红包';
	$data['status']='9';
	$insertid = db_insert(t_commshare, $data);

	$result = array(
		"status"=>5,
		"msg"=>'很遗憾,没有摇到~',
	);
	return_msg($result, '', 'ajax');
}


if($config['iscomm']==0 || $system['fenxiao07'] ==0 || $config['comm42']*100==0 ){
	$result = array(
		"status"=>1,
		"msg"=>'摇一摇未开启',
	);
	return_msg($result, '', 'ajax');
}
if ($config['locationtype']==1 && (empty($GLat) || empty($GLng))){
	$result['errcode'] = 5;
	$result['msg'] = '没有获得地理位置信息，请打开手机gps，再刷新试试！';
	return_msg($result, '', 'ajax');
}


//获取第几天
$pm = array(":themeid"=>$themeid,":openid"=>$_G['openid']);
$lightsum = intval(db_fetchcolumn("SELECT COUNT(*) FROM ".tablename(t_share)."
			WHERE themeid = :themeid and openid = :openid and status = 1",$pm));
$share = db_find("SELECT * FROM ".tablename(t_share)."
				WHERE themeid = :themeid and openid = :openid and status = 1
				order by addtime desc ",$pm);
if (date('Y-m-d') != date('Y-m-d',$share['addtime'])){
	$lightsum=$lightsum+1;
}
//echo "天数:".$lightsum;
if ($op=="get") {
   //  if ($lightsum==1){
   //  	if ($config['comm71type']==0 || ($config['comm71type']==1 && $config['comm711']==0) ){
			// $result['errcode'] = 5;
			// $result['msg'] = '很遗憾,没有摇到~';
			// return_msg($result, '', 'ajax');
   //  	}elseif ($config['comm71type']==1 ){
   //  		$randomfee = floatval(mt_rand($config['comm711']*100,$config['comm712']*100)/100);
   //  	}elseif ($config['comm71type']==2 ){
			// $result['errcode'] = 5;
			// $result['msg'] = '今天转发链接到朋友圈,即可得到红包~';
			// return_msg($result, '', 'ajax');
   //  	}elseif ($config['comm71type']==3 ){
			// $result['errcode'] = 5;
			// $result['msg'] = '未开启~';
			// return_msg($result, '', 'ajax');
   //  	}elseif ($config['comm71type']==4 ){
			// $result['errcode'] = 5;
			// $result['msg'] = '今天转发链接到朋友圈,即可得到礼物~';
			// return_msg($result, '', 'ajax');
   //  	}
   //  }elseif ($lightsum==2) {
   //  	if ($config['comm72type']==0 || ($config['comm72type']==1 && $config['comm721']==0) ){
			// $result['errcode'] = 5;
			// $result['msg'] = '很遗憾,没有摇到~';
			// return_msg($result, '', 'ajax');
   //  	}elseif ($config['comm72type']==1 ){
   //  		$randomfee = floatval(mt_rand($config['comm721']*100,$config['comm722']*100)/100);
   //  	}elseif ($config['comm72type']==2 ){
			// $result['errcode'] = 5;
			// $result['msg'] = '今天转发链接到朋友圈,即可得到红包~';
			// return_msg($result, '', 'ajax');
   //  	}elseif ($config['comm72type']==3 ){
			// $result['errcode'] = 5;
			// $result['msg'] = '未开启~';
			// return_msg($result, '', 'ajax');
   //  	}elseif ($config['comm72type']==4 ){
			// $result['errcode'] = 5;
			// $result['msg'] = '今天转发链接到朋友圈,即可得到礼物~';
			// return_msg($result, '', 'ajax');
   //  	}
   //  }elseif ($lightsum==3) {
   //  	if ($config['comm73type']==0 || ($config['comm73type']==1 && $config['comm731']==0) ){
			// $result['errcode'] = 5;
			// $result['msg'] = '很遗憾,没有摇到~';
			// return_msg($result, '', 'ajax');
   //  	}elseif ($config['comm73type']==1 ){
   //  		$randomfee = floatval(mt_rand($config['comm731']*100,$config['comm732']*100)/100);
   //  	}elseif ($config['comm73type']==2 ){
			// $result['errcode'] = 5;
			// $result['msg'] = '今天转发链接到朋友圈,即可得到红包~';
			// return_msg($result, '', 'ajax');
   //  	}elseif ($config['comm73type']==3 ){
			// $result['errcode'] = 5;
			// $result['msg'] = '未开启~';
			// return_msg($result, '', 'ajax');
   //  	}elseif ($config['comm73type']==4 ){
			// $result['errcode'] = 5;
			// $result['msg'] = '今天转发链接到朋友圈,即可得到礼物~';
			// return_msg($result, '', 'ajax');
   //  	}
   //  }elseif ($lightsum==4) {
   //  	if ($config['comm74type']==0 || ($config['comm74type']==1 && $config['comm741']==0) ){
			// $result['errcode'] = 5;
			// $result['msg'] = '很遗憾,没有摇到~';
			// return_msg($result, '', 'ajax');
   //  	}elseif ($config['comm74type']==1 ){
   //  		$randomfee = floatval(mt_rand($config['comm741']*100,$config['comm742']*100)/100);
   //  	}elseif ($config['comm74type']==2 ){
			// $result['errcode'] = 5;
			// $result['msg'] = '今天转发链接到朋友圈,即可得到红包~';
			// return_msg($result, '', 'ajax');
   //  	}elseif ($config['comm74type']==3 ){
			// $result['errcode'] = 5;
			// $result['msg'] = '未开启~';
			// return_msg($result, '', 'ajax');
   //  	}elseif ($config['comm74type']==4 ){
			// $result['errcode'] = 5;
			// $result['msg'] = '今天转发链接到朋友圈,即可得到礼物~';
			// return_msg($result, '', 'ajax');
   //  	}
   //  }elseif ($lightsum==5) {
   //  	if ($config['comm75type']==0 || ($config['comm75type']==1 && $config['comm751']==0) ){
			// $result['errcode'] = 5;
			// $result['msg'] = '很遗憾,没有摇到~';
			// return_msg($result, '', 'ajax');
   //  	}elseif ($config['comm75type']==1 ){
   //  		$randomfee = floatval(mt_rand($config['comm751']*100,$config['comm752']*100)/100);
   //  	}elseif ($config['comm75type']==2 ){
			// $result['errcode'] = 5;
			// $result['msg'] = '今天转发链接到朋友圈,即可得到红包~';
			// return_msg($result, '', 'ajax');
   //  	}elseif ($config['comm75type']==3 ){
			// $result['errcode'] = 5;
			// $result['msg'] = '未开启~';
			// return_msg($result, '', 'ajax');
   //  	}elseif ($config['comm75type']==4 ){
			// $result['errcode'] = 5;
			// $result['msg'] = '今天转发链接到朋友圈,即可得到礼物~';
			// return_msg($result, '', 'ajax');
   //  	}
   //  }


	$begintime1 = $theme['begintime'];
	$endime1 = $theme['endtime'];
	if($begintime1 > TIMESTAMP){
		$result['errcode'] = 5;
		$result['msg'] = '活动尚未开始，敬请期待~';
		return_msg($result, '', 'ajax');
	}
	if($endime1 < TIMESTAMP ||  $theme['status'] != 1){
		$result['errcode'] = 5;
		$result['msg'] = '活动已经结束啦，感谢您的支持';
		return_msg($result, '', 'ajax');
	}
	
	//失败的提示
	$result['errcode'] = 5;
    $result['msg'] = '很遗憾未摇到,感谢支持!';


	//在服务器别的软件领过的不可以领
	if ($config['comm4dashuju']==0){
		$status= Comm::gethistorycomm($themeid);
		if ($status['error']==1){
			$data['msg']=$status['msg'];
			$data['status']='8';
			$insertid = db_insert(t_commshare, $data);
	        return_msg($result, '', 'ajax');
	    }
	}

	// 1点-6点之前不发放
	if (date("H")<6 and date("H")>0) {
		$data['msg']='1点-6点之间不发放';
		$data['status']='10';
		$insertid = db_insert(t_commshare, $data);
		$result['errcode'] = 1;
        $result['msg'] = '很遗憾未摇到,感谢支持!';
        return_msg($result, '', 'ajax');
	}


	//员工是否有红包
	if ($config['comm4worker']==1){
		$myworker = Worker::getThemeWorkerByOpenid($_G['openid'],$themeid,1);
		if (!empty($myworker)){
			$data['msg']='员工裂变红包关闭';
			$data['status']='11';
			$insertid = db_insert(t_commshare, $data);
	        $result['errcode'] = 5;
	        $result['msg'] = '员工不可参与摇一摇,感谢支持!';
	        return_msg($result, '', 'ajax');
	    }
	}
	

	//判断付款的客户才有红包
	if ($config['comm4ispay']==1){
		$themyrecord = Record::getLastGroupByOpenId($_G['openid'],$themeid);
		if(empty($themyrecord)){
			$data['msg']='只有付款后才可以摇一摇';
			$data['status']='12';
			$insertid = db_insert(t_commshare, $data);
	        $result['errcode'] = 5;
	        $result['msg'] = '很遗憾只有付款后才可以摇一摇,感谢支持!';
	        return_msg($result, '', 'ajax');
	    }
	}

	$fansinfo = db_find ( 'SELECT addtime,islock FROM ' . tablename ( t_fans ) . "
				WHERE uid ='{$fuid}'" );
	if($fansinfo['islock']==1){
		$data['msg']='用户已锁定，无法领裂变红包';
		$data['status']='12';
		$insertid = db_insert(t_commshare, $data);
        return_msg($result, '', 'ajax');
	}
	//10秒内刷新2次以上被锁定
	$sql = 'SELECT count(*) FROM ' . tablename ( t_commshare ) . "
				WHERE uid ='{$fuid}' and addtime > " ;
	$sql10 = TIMESTAMP - 10;
	$sharenum10 = db_fetchcolumn ($sql . $sql10);
	$sql120 = TIMESTAMP - 120;
	$sharenum120 = db_fetchcolumn ($sql . $sql120);
	$sql3600 = TIMESTAMP - 3600;
	$sharenum3600 = db_fetchcolumn ($sql . $sql3600);
	if ( $sharenum10 >= 3 || $sharenum120 >= 10 || $sharenum3600  >= 30) {
		db_query("UPDATE ".tablename(t_fans)." SET islock = '1' WHERE uid ='{$fuid}'" );
		
		$data['msg']='短时间内多次摇一摇被封';
		$data['status']='2';
		$insertid = db_insert(t_commshare, $data);
        return_msg($result, '', 'ajax');
	}

	//打开链接10秒内分享朋友圈,自动锁定
	$addtime2 = TIMESTAMP-$config['comm4yaotime'];
	if($fansinfo['addtime'] > $addtime2  || $fansinfo['islock']==1 || empty($fansinfo)){

		db_query("UPDATE ".tablename(t_fans)." SET islock = '1' WHERE uid ='{$fuid}'" );

		$data['msg']='链接打开'.$addtime2.'秒内摇一摇被封';
		$data['status']='2';
		$insertid = db_insert(t_commshare, $data);
        return_msg($result, '', 'ajax');
	}


	//检测时间
	if (!empty($config['comm4time'])) {
		$comm4time= Comm::getCommshareTime($themeid,$fuid);
		if ($comm4time < $config['comm4time']){
			$data['msg']='离下次摇一摇还有'.($config['comm4time']-$comm4time).'分钟';
			$data['status']='5';
			$insertid = db_insert(t_commshare, $data);
			$result['errcode'] = 5;
    		$result['msg'] = $data['msg'];
	        return_msg($result, '', 'ajax');
	    }
	}

	//检测3小时内成交数与发红包数是否达到要求
	if (!empty($config['comm4type']) || !empty($config['comm4typehb'])){
		$status= Comm::gethourslimit($themeid,$config['comm4type'],$config['comm4typehb']);
		if ($status['error']==1){
			$data['msg']=$status['msg'];
			$data['status']='8';
			$insertid = db_insert(t_commshare, $data);
	        return_msg($result, '', 'ajax');
	    }
	}

	//检测今日已领的个数
	$comm4daynum= Comm::getCommshareToday($themeid,$fuid);
	if ($comm4daynum >= $config['comm4daynum']){
		$data['msg']='今天只可摇'.$config['comm4daynum'].'个红包';
		$data['status']='6';
		$insertid = db_insert(t_commshare, $data);
		$result['errcode'] = 5;
		$result['msg'] = $data['msg'];
        return_msg($result, '', 'ajax');
    }

    //检测已领的总个数
	$comm4sum= Comm::getCommshareAll($themeid,$fuid);
	if ($comm4sum >= $config['comm4sum']){
		$data['msg']='您总计可摇'.$config['comm4sum'].'个红包';
		$data['status']='7';
		$insertid = db_insert(t_commshare, $data);
		$result['errcode'] = 5;
		$result['msg'] = $data['msg'];
        return_msg($result, '', 'ajax');
    }

    //检测每小时限额
	$Numhour= floatval(Comm::getCommshareNumHour($themeid));
	if ($Numhour>=floatval($config['comm4hour'])){
		$data['msg']='红包达小时上限';
		$data['status']='3';
		$insertid = db_insert(t_commshare, $data);
        return_msg($result, '', 'ajax');
    }



	//检测每天限额
	$NumDay= floatval(Comm::getCommshareNumDay($themeid));
	if ($NumDay>=floatval($config['comm4day'])){
		$data['msg']='今日红包已发完';
		$data['status']='3';
		$insertid = db_insert(t_commshare, $data);
        return_msg($result, '', 'ajax');
    }

	//检测同一IP
	if (!empty($config['comm4ip'])) {
		$NumIp= floatval(Comm::getCommshareNumIp($themeid,$ClientIp));
		if ($NumIp>=floatval($config['comm4ip'])){
			$data['msg']='同一网络到上限';
			$data['status']='4';
			$insertid = db_insert(t_commshare, $data);
	        return_msg($result, '', 'ajax');
	    }
	}

	if ($config['locationtype']==1) {
		$City= Comm::getCityDW($GLat, $GLng,$config['comm4city']);
	}else{
		$City= Comm::getCity($system,$ClientIp,$config['comm4city']);
	}
		$data['city']=$City['city'];
	//根据IP检测地区
	if (!empty($config['comm4city']) && $City['status']==0){
		$data['msg']='不在活动地区';
		$data['status']='2';
		$insertid = db_insert(t_commshare, $data);
        return_msg($result, '', 'ajax');
	}

    $insertid = db_insert(t_commshare, $data);

	    $totalpayfee = Order::getRealByThemeIdcomm4($themeid) + $theme['chongzhi'];
		if ($totalpayfee<0){
			$data['msg']='您的余额不足,请联系客服进行充值';
			$insertid = db_insert(t_comm, $data);
	        return_msg($result, '', 'ajax');
		}else{
		    $result['errcode'] = 2;
		    $result['fee'] = $randomfee;
		    return_msg($result, '', 'ajax');
	    }

}elseif ($op=="pyq") {
	$isfahongbao=1;
	$randomfee=0;
   //  if ($lightsum==1){
   //  	if ($config['comm71type']==1 || $config['comm71type']==2 ){
			// $isfahongbao=1;
			// if ($config['comm71type']==2) {
			// 	$randomfee = floatval(mt_rand($config['comm711']*100,$config['comm712']*100)/100);
			// }
   //  	}elseif ($config['comm71type']==3 || $config['comm71type']==4 ){
			// $isfahongbao=2;
   //  	}
   //  }elseif ($lightsum==2) {
   //  	if ($config['comm72type']==1 || $config['comm72type']==2 ){
			// $isfahongbao=1;
			// if ($config['comm72type']==2) {
			// 	$randomfee = floatval(mt_rand($config['comm721']*100,$config['comm722']*100)/100);
			// }
   //  	}elseif ($config['comm72type']==3 || $config['comm72type']==4 ){
			// $isfahongbao=2;
   //  	}
   //  }elseif ($lightsum==3) {
   //  	if ($config['comm73type']==1 || $config['comm73type']==2 ){
			// $isfahongbao=1;
			// if ($config['comm73type']==2) {
			// 	$randomfee = floatval(mt_rand($config['comm731']*100,$config['comm732']*100)/100);
			// }
   //  	}elseif ($config['comm73type']==3 || $config['comm73type']==4 ){
			// $isfahongbao=2;
   //  	}
   //  }elseif ($lightsum==4) {
   //  	if ($config['comm74type']==1 || $config['comm74type']==2 ){
			// $isfahongbao=1;
			// if ($config['comm74type']==2) {
			// 	$randomfee = floatval(mt_rand($config['comm741']*100,$config['comm742']*100)/100);
			// }
   //  	}elseif ($config['comm74type']==3 || $config['comm74type']==4 ){
			// $isfahongbao=2;
   //  	}
   //  }elseif ($lightsum==5) {
   //  	if ($config['comm75type']==1 || $config['comm75type']==2 ){
			// $isfahongbao=1;
			// if ($config['comm75type']==2) {
			// 	$randomfee = floatval(mt_rand($config['comm751']*100,$config['comm752']*100)/100);
			// }
   //  	}elseif ($config['comm75type']==3 || $config['comm75type']==4 ){
			// $isfahongbao=2;
   //  	}
   //  }


  //   if($randomfee>0){
		// $begintime1 = $theme['begintime'];
		// $endime1 = $theme['endtime'];
		// if($begintime1 > TIMESTAMP){
		// 	$result['errcode'] = 5;
		// 	$result['msg'] = '活动尚未开始，敬请期待~';
		// 	return_msg($result, '', 'ajax');
		// }
		// if($endime1 < TIMESTAMP ||  $theme['status'] != 1){
		// 	$result['errcode'] = 5;
		// 	$result['msg'] = '活动已经结束啦，感谢您的支持';
		// 	return_msg($result, '', 'ajax');
		// }

		// //失败的提示
		// $result['errcode'] = 5;
	 //    $result['msg'] = '很遗憾未摇到,感谢支持!';

		// $data = array(
		// 	'uniacid'    => $_G['mpid'],
		// 	'themeid'    => $themeid,
		// 	'type'       => 7,
		// 	'uid'     => $fuid,
		// 	'openid'     => $_G['openid'],
		// 	'addtime'    => TIMESTAMP,
		// 	'fee'        => $randomfee,
		// 	'ip'    => $ClientIp,
		// 	'status'     =>0,
		// );

		// //在服务器别的软件领过的不可以领
		// if ($config['comm4dashuju']==0){
		// 	$status= Comm::gethistorycomm($themeid);
		// 	if ($status['error']==1){
		// 		$data['msg']=$status['msg'];
		// 		$data['status']='8';
		// 		$insertid = db_insert(t_commshare, $data);
		//         return_msg($result, '', 'ajax');
		//     }
  //   	}
    	
		// // 7点之前不发放
		// if (date("H")<7) {
		// 	$result['errcode'] = 1;
	 //        $result['msg'] = '很遗憾未摇到,感谢支持!';
	 //        return_msg($result, '', 'ajax');
		// }


		// //员工是否有红包
		// if ($config['comm4worker']==1){
		// 	$myworker = Worker::getThemeWorkerByOpenid($_G['openid'],$themeid,1);
		// 	if (!empty($myworker)){
		//         $result['errcode'] = 5;
		//         $result['msg'] = '员工不可参与摇一摇,感谢支持!';
		//         return_msg($result, '', 'ajax');
		//     }
		// }
		

		// //判断付款的客户才有红包
		// if ($config['comm4ispay']==1){
		// 	$themyrecord = Record::getLastGroupByOpenId($_G['openid'],$themeid);
		// 	if(empty($themyrecord)){
		//         $result['errcode'] = 5;
		//         $result['msg'] = '很遗憾只有付款后才可以摇一摇,感谢支持!';
		//         return_msg($result, '', 'ajax');
		//     }
		// }

		// $fansinfo = db_find ( 'SELECT addtime,islock FROM ' . tablename ( t_fans ) . "
		// 			WHERE uid ='{$fuid}'" );
		// if($fansinfo['islock']==1){
	 //        return_msg($result, '', 'ajax');
		// }
		// //10秒内刷新2次以上被锁定
		// $sql = 'SELECT count(*) FROM ' . tablename ( t_commshare ) . "
		// 			WHERE uid ='{$fuid}' and addtime > " ;
		// $sql10 = TIMESTAMP - 10;
		// $sharenum10 = db_fetchcolumn ($sql . $sql10);
		// $sql120 = TIMESTAMP - 120;
		// $sharenum120 = db_fetchcolumn ($sql . $sql120);
		// $sql3600 = TIMESTAMP - 3600;
		// $sharenum3600 = db_fetchcolumn ($sql . $sql3600);
		// if ( $sharenum10 >= 2 || $sharenum120 >= 10 || $sharenum3600  >= 30) {
		// 	db_query("UPDATE ".tablename(t_fans)." SET islock = '1' WHERE uid ='{$fuid}'" );
			
		// 	$data['msg']='短时间内多次转发被封';
		// 	$data['status']='2';
		// 	$insertid = db_insert(t_commshare, $data);
	 //        return_msg($result, '', 'ajax');
		// }

		// //打开链接30秒内分享朋友圈,自动锁定
		// $addtime2=TIMESTAMP-$config['comm4sharetime'];
		// if($fansinfo['addtime'] > $addtime2  || $fansinfo['islock']==1 || empty($fansinfo)){

		// 	db_query("UPDATE ".tablename(t_fans)." SET islock = '1' WHERE uid ='{$fuid}'" );

		// 	$data['msg']='链接打开'.$config['comm4sharetime'].'秒内转发被封';
		// 	$data['status']='2';
		// 	$insertid = db_insert(t_commshare, $data);
	 //        return_msg($result, '', 'ajax');
		// }


		// //检测时间
		// if (!empty($config['comm4time'])) {
		// 	$comm4time= Comm::getCommshareTime($themeid,$fuid);
		// 	if ($comm4time < $config['comm4time']){
		// 		$data['msg']='离下次摇一摇还有'.($config['comm4time']-$comm4time).'分钟';
		// 		$data['status']='5';
		// 		$insertid = db_insert(t_commshare, $data);
		// 		$result['errcode'] = 5;
	 //    		$result['msg'] = $data['msg'];
		//         return_msg($result, '', 'ajax');
		//     }
		// }
		
		// //检测3小时内成交数与发红包数是否达到要求
		// if (!empty($config['comm4type']) || !empty($config['comm4typehb'])){
		// 	$status= Comm::gethourslimit($themeid,$config['comm4type'],$config['comm4typehb']);
		// 	if ($status['error']==1){
		// 		$data['msg']=$status['msg'];
		// 		$data['status']='8';
		// 		$insertid = db_insert(t_commshare, $data);
		//         return_msg($result, '', 'ajax');
		//     }
		// }

		// //检测今日已领的个数
		// $comm4daynum= Comm::getCommshareToday($themeid,$fuid);
		// if ($comm4daynum >= $config['comm4daynum']){
		// 	$data['msg']='今天只可摇'.$config['comm4daynum'].'个红包';
		// 	$data['status']='6';
		// 	$insertid = db_insert(t_commshare, $data);
		// 	$result['errcode'] = 5;
		// 	$result['msg'] = $data['msg'];
	 //        return_msg($result, '', 'ajax');
	 //    }

	 //    //检测已领的总个数
		// $comm4sum= Comm::getCommshareAll($themeid,$fuid);
		// if ($comm4sum >= $config['comm4sum']){
		// 	$data['msg']='您总计可摇'.$config['comm4sum'].'个红包';
		// 	$data['status']='7';
		// 	$insertid = db_insert(t_commshare, $data);
		// 	$result['errcode'] = 5;
		// 	$result['msg'] = $data['msg'];
	 //        return_msg($result, '', 'ajax');
	 //    }

	 //    //检测每小时限额
		// $Numhour= floatval(Comm::getCommshareNumHour($themeid));
		// if ($Numhour>=floatval($config['comm4hour'])){
		// 	$data['msg']='红包达小时上限';
		// 	$data['status']='3';
		// 	$insertid = db_insert(t_commshare, $data);
	 //        return_msg($result, '', 'ajax');
	 //    }


		// //检测每天限额
		// $NumDay= floatval(Comm::getCommshareNumDay($themeid));
		// if ($NumDay>=floatval($config['comm4day'])){
		// 	$data['msg']='今日红包已发完';
		// 	$data['status']='3';
		// 	$insertid = db_insert(t_commshare, $data);
	 //        return_msg($result, '', 'ajax');
	 //    }

		// //检测同一IP
		// if (!empty($config['comm4ip'])) {
		// 	$NumIp= floatval(Comm::getCommshareNumIp($themeid,$ClientIp));
		// 	if ($NumIp>=floatval($config['comm4ip'])){
		// 		$data['msg']='同一网络到上限';
		// 		$data['status']='4';
		// 		$insertid = db_insert(t_commshare, $data);
		//         return_msg($result, '', 'ajax');
		//     }
		// }

		// if ($config['locationtype']==1) {
		// 	$City= Comm::getCityDW($GLat, $GLng,$config['comm4city']);
		// }else{
		// 	$City= Comm::getCity($system,$ClientIp,$config['comm4city']);
		// }
		// $data['city']=$City['city'];
		
		// //根据IP检测地区
		// if (!empty($config['comm4city']) && $City['status']==0){
		// 	$data['msg']='不在活动地区';
		// 	$data['status']='2';
		// 	$insertid = db_insert(t_commshare, $data);
	 //        return_msg($result, '', 'ajax');
		// }

	 //    $insertid = db_insert(t_commshare, $data);
  //   }

    if($isfahongbao==1){
		$weizhifulist = db_find ( 'SELECT city,fee FROM ' . tablename ( t_commshare ) . "
					WHERE themeid = '{$themeid}' and status = 0 and uid ='{$fuid}'" );
		$weizhifu = $weizhifulist['fee'];
		if ($weizhifu>=0.3 || ($config['comm4autored']==3 && $weizhifu>0)){

			$ordersn = getMillisecond();
			$data = array(
				'uniacid'    => $_G['mpid'],
				'themeid'    => $themeid,
				'recordid'   => '',
				'ordersn'    => $ordersn,
				'type'       => 7,
				'uid'     => $fuid,
				'openid'     => $_G['openid'],
				'addtime'    => TIMESTAMP,
				'fee'        => $weizhifu,
				'ip'    => $ClientIp,
				'city'    => $weizhifulist['city'],
				'fromuid'    => 0,
				'status'     =>0,
			);
			$data2= $data;
		    $data2['software'] = MODULE_NAME;
		    $insertid = db_insert('2019comm_list', $data2);

	        //1分钟内只能收到一个红包
	        if (empty($weizhifulist['addtime'])) {
	        	$addtime =  TIMESTAMP - 60;
	        }else{
	        	$addtime =  $weizhifulist['addtime'] - 60;
	        }
	        $p = array(':themeid' => $data['themeid'],':uid' => $data['uid'],':type' => $data['type'],':addtime' => $addtime );        
	        $sql = "SELECT * FROM " . tablename (t_comm) . " WHERE themeid= :themeid and uid= :uid and type= :type and addtime > :addtime" ;
	        $commrow = db_find($sql,$p);
	        if (!empty($commrow)){			
				db_query("UPDATE ".tablename(t_commshare)." SET status = 5, msg = '操作频繁：1分钟内只能收到一个红包'
					WHERE themeid = '{$themeid}' and status = 0 and uid ='{$fuid}'" );
				$result['errcode'] = 5;
				$result['msg'] = '感谢支持';
		        return_msg($result, '', 'ajax');
			}else{				
				//将所有未支付记录更新为正常
				db_query("UPDATE ".tablename(t_commshare)." SET status = 1, sendtime = ".TIMESTAMP."
					WHERE themeid = '{$themeid}' and status = 0 and uid ='{$fuid}'" );
			}


			if($system['autored']== 0 || $config['comm4autored']== 0 || ($config['comm4autored']== 2 && TIMESTAMP > $config['comm4autoredtime']) ){	
				$data['msg']='等待管理员发红包';
				$insertid = db_insert(t_comm, $data);
				$comm4tips = str_replace("#fee",$weizhifu,$config['comm4tips']);
				$result['errcode'] = 5;
				$result['msg'] = $comm4tips;
			    $result['fee'] = $randomfee;
			    $result['fee2'] =$weizhifu;
		        return_msg($result, '', 'ajax');
		    }


		    if($config['comm4autored']== 3){				
				$data['msg']='待客户提现';

				$pm = array(":themeid"=>$themeid,":openid"=>$_G['openid']);
				$totalfail = db_fetchcolumn ( 'SELECT sum(fee) FROM ' . tablename (t_comm) . ' as m 
				            WHERE m.themeid = :themeid and status=0 and type<>99 and m.openid = :openid',$pm);
				$totalsuccess = db_fetchcolumn ( 'SELECT sum(fee) FROM ' . tablename (t_comm) . ' as m 
			            WHERE m.themeid = :themeid and (status=1 or status=0 and type=99) and m.openid = :openid',$pm);

				$chae = $config['hbtixian']-$totalfail-$weizhifu;
				if ($chae<0 || ($totalsuccess>0 && $config['hbtixianmode'] == 0)) {
					$comm4tips = "恭喜您获得红包" . $weizhifu . "元，可在【我的红包】中提现";
				}else{
					$comm4tips = "恭喜您获得红包" . $weizhifu . "元，再有".$chae."元就可提现了";
				}
				$result['errcode'] = 5;
				$result['msg'] = $comm4tips;
				$insertid = db_insert(t_comm, $data);
		        return_msg($result, '', 'ajax');
		    }

		    $totalpayfee = Order::getRealByThemeIdcomm4($themeid) + $theme['chongzhi'];
			if ($totalpayfee<0){
				$data['msg']='您的余额不足,请联系客服进行充值';
				$insertid = db_insert(t_comm, $data);
		        return_msg($result, '', 'ajax');
			}

			$insertid = db_insert(t_comm, $data);

		    $row = Comm::getCommyaoyiyao($data);
		    $r = array();
		    if(!empty($row) && $row['fee']>=0.3){
		        // $cachekey = 'comm'.$_G['mpid'].MODULE_NAME.$row['id'];
		        // $settings = cache_load($cachekey);
		        // if(empty($settings)){
		            $settings = "commid:".$row['id'];
		            // cache_write($cachekey, $settings);
		            $r = Fee::commRecord($row,$system);
		            if ($r[0]==0){
					    $result['errcode'] = 2;
					    $result['fee'] = $randomfee;
					    $result['fee2'] =$weizhifu;
					    return_msg($result, '', 'ajax');
		            }
		        // }
		    }else{
		        $result['errcode'] = 1;
		        $result['msg'] = '感谢支持!';
		        return_msg($result, '', 'ajax');
		    }
		}else{
		    $result['errcode'] = 1;
		    $result['msg'] = '感谢支持!';
		    return_msg($result, '', 'ajax');
		}
	}else{
	    $result['errcode'] = 1;
	    $result['msg'] = '感谢支持!';
	    return_msg($result, '', 'ajax');
	}
}elseif ($op=="get1") {

	$theme = db_find("SELECT * FROM ".tablename($this->themetable)." WHERE state<5 and id= '{$themeid}'");

	$fee = db_fetchcolumn('SELECT sum(round(price,2)) FROM ' . tablename ( $this->yaoyiyaotable ) . " WHERE state = 1 and yaoyiyaogroupid = {$system['yaoyiyaogroupid']} ");

	$fee=floatval($fee);
	if ($fee==0){
		$result = array(
				"status"=>1,
				"mess"=>'摇一摇礼品未配置',
		);
		die(json_encode($result));
	}
	$suiji=rand(1,$fee);//取随机数
		
	$yaoyiyaosql="SELECT * FROM " . tablename ( $this->yaoyiyaotable ) . " WHERE state = 1 and yaoyiyaogroupid = {$system['yaoyiyaogroupid']}";
	$yaoyiyao = db_select ( $yaoyiyaosql );


	$min=0;$max=0;
	foreach ($yaoyiyao as $index => $item){
		if ($item['price']>0){
			$max=$max+$item['price'];
			if (($suiji>$min) && ($suiji<=$max)){

				$data = array(
						'uniacid'=>$_G['mpid'],
						'themeid'=>$themeid,
						'fansid'=>$fans['id'],
						'time'=>date("Y-m-d H:i:s"),
						'name'=>$item['name'],
						'photo'=>$item['photo'],
						'workerid'=>$theme['workerid'],
						); 

				$insertid = db_insert($this->lingqutable, $data);

				$result=array(
					'status'=>"success",
					'mess'=>$item['name']
					); 
				die(json_encode($result));
			}
		}
		$min=$max;
	}
	$result = array(
			"status"=>1,
			"mess"=>$fee.'-'.$suiji.'遗憾，没有摇到礼品',
	);
	die(json_encode($result));
}else{

		global $_P, $_G;
		$gongneng= 'weike842';
		$system=$_G['system'];
		$fans = $this->checkinfo();
		$themeid = intval($_P['themeid']);

		if($themeid<=0){
			return_msg('id is null');
		}
		
		$theme = db_find("SELECT * FROM ".tablename($this->themetable)." WHERE state<5 and id= '{$themeid}'");
		$lingqu = db_find("SELECT * FROM ".tablename($this->lingqutable)." WHERE fansid={$fans['id']}");

        $imgs = unserialize($theme['dingbu']);
        if (empty($theme['fxphoto'])){
			if (!empty($imgs)){
				foreach ($imgs as $index => $item){
					$fximg=$_G['attachurl'].$item;
					break;
				}
			}else{
				$fximg='http://css.yixuan.net/weike/images/jrkp.jpg';
			}
		}else{
			$fximg=$_G['attachurl'].$theme['fxphoto'];
		}

        $temptxt = $theme['fxbiaoti'];
        $temptxt=str_replace("#customer",$theme['kehuxingming'],$temptxt);
        $temptxt=str_replace("#shopname",$system['dianming'],$temptxt);
        if ($fans['fname'] !='') {
            $temptxt=str_replace("#fans",$fans['fname'],$temptxt);
        }else{
            $temptxt=str_replace("#fans",$fans['nickname'],$temptxt);
        }
        $temptxt=str_replace("#star",$theme['xingxingshu'],$temptxt);
        $temptxt=str_replace("#gift",$theme['liwushu'],$temptxt);
        $temptxt=str_replace("#gold",$theme['jinbishu'],$temptxt);
        $fxbiaoti=$temptxt;



        $temptxt = $theme['fxmiaoshu'];
        $temptxt=str_replace("#customer",$theme['kehuxingming'],$temptxt);
        $temptxt=str_replace("#shopname",$system['dianming'],$temptxt);
        if ($fans['fname'] !='') {
            $temptxt=str_replace("#fans",$fans['fname'],$temptxt);
        }else{
            $temptxt=str_replace("#fans",$fans['nickname'],$temptxt);
        }
        $temptxt=str_replace("#star",$theme['xingxingshu'],$temptxt);
        $temptxt=str_replace("#gift",$theme['liwushu'],$temptxt);
        $temptxt=str_replace("#gold",$theme['jinbishu'],$temptxt);
        $fxmiaoshu=$temptxt;

		$return_clicent = get_defined_vars();
return view('/fudai', $return_clicent);
	}
}
}